package com.liujianlei.forum_system.service.impl;

import com.liujianlei.forum_system.common.AppResult;
import com.liujianlei.forum_system.common.ResultCode;
import com.liujianlei.forum_system.dao.MessageMapper;
import com.liujianlei.forum_system.exception.ApplicationException;
import com.liujianlei.forum_system.model.Message;
import com.liujianlei.forum_system.service.IMessageService;
import com.liujianlei.forum_system.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author tongchen
 * @create 2023-08-11 10:35
 */
@Slf4j
@Service
public class MessageServicceImpl implements IMessageService {
  @Resource
  private MessageMapper messageMapper;

    @Override
    public void createMessage(Message message) {
      //检查数据的有效性
      if(message==null||message.getPostUserId()==null||message.getPostUserId()==null||StringUtils.isEmpty(message.getContent())){
        log.error("内容为空");
        throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
      }
      //补充其他数据
      message.setDeleteState((byte) 0);
      message.setState((byte) 0);
      Date date = new Date();
      message.setCreateTime(date);
      message.setUpdateTime(date);
      //调用mapper进行数据的保存
      int result = messageMapper.insertSelective(message);
      if(result!=1){
        throw new ApplicationException(AppResult.failed(ResultCode.ERROR_INSERT));
      }
    }

  @Override
  public Integer getUnreadCount(Long receiveUserId) {
      //校验参数
    if(receiveUserId==null||receiveUserId<0){
      throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
    }
    //调用mapper
    Integer unreadCount = messageMapper.getUnreadCount(receiveUserId);
    if(unreadCount==null){
      throw new ApplicationException(AppResult.failed(ResultCode.ERROR_UPDATE));
    }
    //返回结果
      return unreadCount;
  }

  @Override
  public List<Message> selectByReceiveUserId(Long receiveUserId) {
      //判断参数的有效性
    if(receiveUserId==null||receiveUserId<0){
      //直接报错
      log.warn("参数不符合要求");
      throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
    }
    //调用方法直接返回
    List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
    return messages;
  }
  @Override
  public Message selectById(Long id) {
    // 非空校验
    if (id == null || id <= 0) {
      // 打印日志
      log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
      // 抛出异常
      throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
    }
    // 调用DAO
    Message message = messageMapper.selectByPrimaryKey(id);
    // 返回结果
    return message;
  }

  @Override
  public void reply(Long replyMessageId, Message message) {
      if(replyMessageId==null||replyMessageId<0){
        log.error("当前信息对应id不存在");
        throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
      }
      //获取需要回复的信息
    Message replyMessage = messageMapper.selectByPrimaryKey(replyMessageId);
    //检查信息有效性
    if(replyMessage==null||replyMessage.getDeleteState()==1){
      //报错
      log.error("当前信息不存在");
      throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
    }
      //更新信息状态
    replyMessage.setState((byte) 2);
    //调用mapper方法实现回复信息的创建
    createMessage(message);
  }

  @Override
  public void updateStateById(Long id, Byte state) {
    // 非空校验, state : 0 未读 1 已读 2 已回复
    if (id == null || id <= 0 || state < 0 || state > 2) {
      // 打印日志
      log.error(ResultCode.FAILED_PARAMS_VALIDATE.toString());
      // 抛出异常
      throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
    }
    //利用在数据库中查询数据
    Message message = messageMapper.selectByPrimaryKey(id);
    if(message==null){
      log.error("该信息不存在");
      throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
    }
    //将state进行修改
    //判断是否需要修改
    Byte states = message.getState();
    if(states!=state){
      //修改更新时间
      Date date = new Date();
      message.setUpdateTime(date);
      message.setState(state);
      Integer result = messageMapper.updateByPrimaryKeySelective(message);
      if(result!=1){
        //抛出异常
        log.error("信息状态修改失败");
        throw new ApplicationException(AppResult.failed(ResultCode.ERROR_UPDATE));
      }
    }

  }

}
